<template>
    <van-popup
        position="left"
        :value="value"
        closeable
        @click-close-icon='$emit("input", false);isEdit=false'
    >
    <div class="article-channel">
      <div class="head">
        <h3>我的频道<small>点击进入频道</small></h3>
        <a class="edit" href="javascript:;" :class="{active:isEdit}" @click="isEdit=!isEdit">{{isEdit?'完成':'编辑'}}</a>
      </div>
      <div class="body">
        <a :class="{active: activeIndex===index}" @click="enterChannel(index)" href="javascript:;" v-for="(channel,index) in myChannels" :key="channel.id">
          {{channel.name}}
          <geek-icon @click.native.stop="deleteChannel(channel.id, index)" v-show="isEdit && activeIndex!==index && index!==0" name="tag-close"></geek-icon>
        </a>

      </div>
      <div class="head" style="margin-top:12px">
        <h3>频道推荐</h3>
      </div>
      <div class="body">
        <a @click="addChannel(channel)" href="javascript:;" v-for="channel in optionChannels" :key="channel.id">+{{channel.name}}</a>
      </div>
    </div>
  </van-popup>
</template>
<script>
import { getAllChannels, addChannel, deleteChannel } from '@/api/home'
export default {
  props: ['value', 'myChannels', 'activeIndex'],
  methods: {
    // 删除频道
    async deleteChannel (id, index) {
      await deleteChannel(id)

      // 如果删除成功 清除在mychannels中的数据
      this.myChannels.splice(index, 1)
    },
    // 增加频道
    async addChannel (channel) {
      // 1.拼接api函数中需要使用到数组数据
      const tempChannels = JSON.parse(JSON.stringify(this.myChannels))
      tempChannels.push({
        id: channel.id,
        name: channel.name
      })

      tempChannels.forEach((item, index) => {
        item.seq = index + 1
      })
      // 特别注意，往这个函数里面传递到是什么数据？
      console.log(tempChannels)
      await addChannel(tempChannels)
      this.myChannels.push({
        id: channel.id,
        name: channel.name
      })
    },
    enterChannel (index) {
      // 1.去掉弹出层
      this.$emit('input', false)

      // 2.让父组件的activeIndex发生变化
      this.$emit('update:activeIndex', index)
    }
  },
  data () {
    return {
      allChannels: [],
      isEdit: false
    }
  },
  async created () {
    const [err, data] = await getAllChannels()
    if (err) return this.$toast.fail(err)
    this.allChannels = data.data.data.channels
  },
  computed: {
    optionChannels () {
      // 过滤
      return this.allChannels.filter((item) => {
        //   返回在myChannels 中找不到相关id的数据
        return !this.myChannels.find((item2) => item2.id === item.id)
      })
    }
  }
}
</script>

<style scoped lang="less">
.van-popup {
  width: 100%;
  height: 100%;
  ::v-deep .van-popup__close-icon {
    font-size: 20px;
    right: 12px;
    top: 12px;
  }
}
.article-channel {
  margin-top: 44px;
}

.article-channel {
  margin-top: 44px;
  .head {
    padding: 0 16px;
    display: flex;
    justify-content: space-between;
    justify-items: center;
    padding-bottom: 12px;
    h3 {
      font-size: 16px;
      color: #333;
      margin: 0;
      small {
        font-size: 12px;
        color: #999;
        margin-left: 10px;
      }
    }
    .edit {
      float: right;
      height: 22px;
      width: 52px;
      line-height: 22px;
      text-align: center;
      color: #DE644B;
      border-radius: 11px;
      border: 1px solid #DE644B;
      font-size: 12px;
      &.active {
        color: #fff;
        background: #DE644B;
      }
    }
  }
  .body {
    padding: 10px 6px 0 16px;
    a {
      display: inline-block;
      padding: 0 8px;
      font-size:14px;
      color: #3A3948;
      background: #F7F8FA;
      height: 36px;
      line-height: 36px;
      min-width: 78px;
      margin-right: 10px;
      margin-bottom: 12px;
      border-radius: 18px;
      text-align: center;
      position: relative;
      .geek-icon {
        position: absolute;
        top: -5px;
        right: -5px;
        line-height: 1;
      }
      &.active {
        color: @geek-color;
      }
    }
  }
}
</style>
